/**
 * @file    pc_jbd.c
 * @brief   上位机通信协议-JBD协议
 * @author  liuwei 
 * @version 1.0.1
 * @date    2023-06-19
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-06-19 <td>1.0.1     <td>Liuwei   <td>首次创建
 * <tr><td>2023-01-03 <td>1.0.2     <td>Liuwei   <td>添加BarCode码
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "wifi_blue.h"
#include "cfg_prj.h"
#include "var_mng.h"
#include "param_mng.h"
#include "diagnosis.h"
#include <string.h>
#include "sample.h"
#include "modbus_app.h"
#include "parallel.h"
#include "base_math.h"

/* macro/define --------------------------------------------------------------*/
#define MSG_FRAME_HARD    (0x3B3C)
#define FRAME_END         (0x0D)
/* typedef -------------------------------------------------------------------*/
typedef struct 
{
    uint8_t hand;
    uint8_t dev_addr;
    uint8_t wr_cmd;
    uint8_t reg_cmd;
    uint8_t len;
    uint8_t data_buf[50];
    uint16_t crc;
    uint8_t end;
}pc_jbd_data_type;

typedef struct 
{
	uint8_t cmd;
	uint8_t(*p_call_func)(pc_jbd_data_type , uint8_t* , uint16_t*);
}wifi_cmd_type;

/* local functions -----------------------------------------------------------*/
static uint8_t wifi_read_cmd_01_deal(pc_jbd_data_type recv_data, uint8_t *tx_buf, uint16_t *tx_num);
/* static variables ----------------------------------------------------------*/
static wifi_cmd_type read_cmd_list_map[] = 
{
	{0x01 , wifi_read_cmd_01_deal } ,
};

#define CMD_READ_MAP_SIZE  (sizeof(read_cmd_list_map)/sizeof(wifi_cmd_type))
	
//static jbd_cmd_type write_cmd_list_map[] = 
//{
//	{0x00 , jbd_write_cmd_unlock_deal },
//	{0x01 , jbd_write_cmd_lock_deal },
//	{0x0A , jbd_write_cmd_0A_deal } ,
//	{0x0B , jbd_write_cmd_0B_deal } ,
//	{0x0C , jbd_write_cmd_0C_deal } ,
//	{0xE1 , jbd_write_cmd_E1_deal } ,
//};


//#define CMD_WRITE_MAP_SIZE  (sizeof(write_cmd_list_map)/sizeof(jbd_cmd_type))
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
uint8_t wifi_blue_com_analysis(uint8_t *rx_buf , uint16_t rx_len ,uint8_t *tx_buf , uint16_t *tx_len)
{
    uint16_t calc_crc = 0;
    uint16_t i = 0;
    uint8_t res = FALSE;
	uint16_t frame_hand = 0;
	uint8_t cmd = 0;
    pc_jbd_data_type recv_data = {0};
    if(rx_len < 5)
        return FALSE;
    
	frame_hand = (rx_buf[0] << 8) + rx_buf[1];
    if((frame_hand == MSG_FRAME_HARD) && (rx_buf[rx_len - 1] == FRAME_END))
    {
        recv_data.crc = (rx_buf[rx_len - 3] << 8) + rx_buf[rx_len - 2];
		calc_crc = modbus_calc_crc(&rx_buf[2] , 10);
        if(recv_data.crc == calc_crc)
        {
			cmd = rx_buf[10];
			for( i = 0;i < CMD_READ_MAP_SIZE; ++ i)
			{
				if(cmd == read_cmd_list_map[i].cmd)
					res = read_cmd_list_map[i].p_call_func(recv_data , tx_buf , tx_len);
			}
        }
    }

    return res;
}
/** ------------------------------------------------------------------------- *
  *                             Local function
 ** ------------------------------------------------------------------------- */
static uint8_t wifi_read_cmd_01_deal(pc_jbd_data_type recv_data, uint8_t *tx_buf, uint16_t *tx_num)
{
    uint16_t tx_idx = 0;
    uint16_t calc_crc = 0;
	uint8_t i = 0;
	uint8_t len = 0;
	uint8_t hard_type[20] = HARD_PLATFORM;
	uint8_t soft_type[20] = PRJ_NAME;
	uint16_t val = 0;
	
    tx_buf[tx_idx ++ ] = MSG_FRAME_HARD >> 8;
	tx_buf[tx_idx ++ ] = MSG_FRAME_HARD & 0xff;
	tx_buf[tx_idx ++ ] = 0x01;
	if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
	{
		tx_buf[tx_idx ++ ] = master_data_reg.parallel_num - 1;
	}
	else
	{
		tx_buf[tx_idx ++ ] = 0x00;
	}
	tx_buf[tx_idx ++ ] = 0x00;
	tx_buf[tx_idx ++ ] = 0x00;
	tx_buf[tx_idx ++ ] = 0x00;
	tx_buf[tx_idx ++ ] = 0x00;
	tx_buf[tx_idx ++ ] = 0x00;
	tx_buf[tx_idx ++ ] = 0x00;
	tx_buf[tx_idx ++ ] = 0x01;
	
	tx_buf[tx_idx ++ ] = 0x00;
	
	tx_buf[tx_idx ++ ] = 0x01;
	tx_buf[tx_idx ++ ] = 0x04;
	if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
		tx_buf[tx_idx ++ ] = 0x03;
	else
		tx_buf[tx_idx ++ ] = 0x07;
	tx_buf[tx_idx ++ ] = 0x02;
	tx_buf[tx_idx ++ ] = 0x35;
	if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
	{
		if(math_get_data_bit1_num(master_data_reg.parallel_num) > 1) 
			tx_buf[tx_idx ++ ] = 0x02;
		else
			tx_buf[tx_idx ++ ] = 0x00;
	}
	else
		tx_buf[tx_idx ++ ] = 0x00;
	
	tx_buf[tx_idx ++ ] = 0x02;
	len = strlen((char *)&hard_type);
	tx_buf[tx_idx ++ ] = len;
	for( i = 0;i < len; ++ i)
	{
		tx_buf[tx_idx ++ ] = hard_type[i];
	}

	tx_buf[tx_idx ++ ] = 0x03;
	len = strlen((char *)&soft_type);
	tx_buf[tx_idx ++ ] = len + 9;
	for( i = 0;i < len; ++ i)
	{
		tx_buf[tx_idx ++ ] = soft_type[i];
	}
	tx_buf[tx_idx ++ ] = '.';
	tx_buf[tx_idx ++ ] = SOFT_VER_MAIN /10 + 0x30;
	tx_buf[tx_idx ++ ] = SOFT_VER_MAIN % 10 + 0x30;
	tx_buf[tx_idx ++ ] = '.';
	tx_buf[tx_idx ++ ] = SOFT_VER_RELEASE /10 + 0x30;
	tx_buf[tx_idx ++ ] = SOFT_VER_RELEASE % 10 + 0x30;
	tx_buf[tx_idx ++ ] = '.';
	tx_buf[tx_idx ++ ] = SOFT_VER_TEST /10 + 0x30;
	tx_buf[tx_idx ++ ] = SOFT_VER_TEST % 10 + 0x30;
	
	tx_buf[tx_idx ++ ] = 0x04;
	tx_buf[tx_idx ++ ] = 0x1E;
	for( i = 0;i < 0x0F; ++ i)
	{
		val = param_get_data(PAR_ID_SN_CODE + i);
		tx_buf[tx_idx ++ ] = val & 0xff;
		tx_buf[tx_idx ++ ] = val >> 8;
	}
	
	tx_buf[11] = tx_idx - 12;
	
	calc_crc = modbus_calc_crc(&tx_buf[2] , tx_idx - 2);
    tx_buf[tx_idx ++ ] = calc_crc >> 8;
    tx_buf[tx_idx ++ ] = calc_crc & 0xff;
	
    tx_buf[tx_idx ++ ] = FRAME_END;
	
	*tx_num = tx_idx;
	
    return TRUE;
}

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


